package base.thread;

public class DeadLock implements Runnable {
	
	private static Object object1 = new Object();
	private static Object object2 = new Object();
	private boolean flag=true;

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public void run() {
		if (flag) {
			synchronized (object1) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("just object1");
				synchronized (object2) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("object1--object2--over");
				}
			}
		} else {
			synchronized (object2) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("just object2");
				synchronized (object1) {
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println("object2--object1--over");
				}
			}
		}
	}
	
	
	public static void main(String[] args) {
		DeadLock l1=new DeadLock();
		l1.setFlag(true);
		DeadLock l2=new DeadLock();
		l2.setFlag(false);
		
		Thread t1=new Thread(l1);
		Thread t2=new Thread(l2);
		t1.start();
		t2.start();
		System.out.println("hhh");
		
	}
}
